package com.etc.traval.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.etc.traval.biz.RoomBiz;
import com.etc.traval.biz.SellerBiz;
import com.etc.traval.bizimpl.RoomBizImpl;
import com.etc.traval.bizimpl.SellerBizImpl;
import com.etc.travel.entity.Room;
import com.etc.travel.entity.Seller;
import com.etc.travel.entity.User;

public class RoomServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public RoomServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		RoomBiz biz=new RoomBizImpl();
		String flag = request.getParameter("flag");
		SellerBiz sellerbiz = new SellerBizImpl();
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		if (request.getParameter("flag") != null) {
			flag = request.getParameter("flag");
		}
		if (flag.equals("addroom")) {
			
			Room room=new Room();
			Seller seller=sellerbiz.selectSellerById(Integer.valueOf(request.getParameter("sellerid")));
			String logo=request.getParameter("mylogo");
			Double money=Double.valueOf(request.getParameter("rmoney"));
			String type=request.getParameter("rtype");
			String system="可预订";
			room.setRoom_logo(logo);
			room.setRoom_money(money);
			room.setRoom_seller(seller);
			room.setRoom_system(system);
			room.setRoom_type(type);
			if(biz.addRoom(room)){
				out.print("<script>alert('添加成功'); location.href='SellerManage.jsp';</script>");				
			}else {
				out.print("<script>alert('添加失败	'); location.href='SellerManage.jsp';</script>");
			}
			out.flush();
			out.close();
		}else if(flag.equals("delete")){
			int id=Integer.valueOf(request.getParameter("roomid"));
			if(biz.delRoom(id)){
				out.print("<script>alert('删除成功'); location.href='SellerManage.jsp';</script>");
			}else{
				out.print("<script>alert('删除失败	'); location.href='SellerManage.jsp';</script>");
			}
			
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
